package guo.controller;

import guo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: gsq
 * @description: TODO
 * @date: 2024/5/11 14:55
 * @version: 1.0
 */
@RestController
public class XmlToDataController {
    /**
     *@ClassName: StudentController
     *@Description: 上传excel
     *@Params: file
     *@Return:
     *@Author xxw
     *@Date 2020/12/15
     */

    @Autowired
    private StudentService studentService;

    @PostMapping("/uploadExcel")
    public String uploadExcel(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename(); // 获取文件名
//  得到 .jpg/.png/.xlsx
        String substring = fileName.substring(fileName.lastIndexOf("."));
//        new HashMap<String,String>()
        InputStream is = null;
        try{
            is = file.getInputStream();
            List<Map> studentList = studentService.getListByExcel(is,fileName);// 获取解析后的List集合
            studentList.remove(0);

            // System.out.println(studentList.toString());
            Boolean result = studentService.batchImportStuInfo(studentList); // 把数据插入数据库
            if (result){
                return "文件上传成功！";
            }else {
                return "文件上传失败";
            }
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            is.close();
        }
        return "错误";
    }

}
